package com.tian.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.tian.mapper.PatientMapper;

import com.tian.pojo.Patient;
import com.tian.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class PatientServiceImpl implements PatientService{
    @Autowired
    private PatientMapper patientMapper;

    @Override
    public PageResult getUserList(PageResult pageResult) {
        //1.定义分页对象
        IPage<Patient> page = new Page<>(pageResult.getPageNum(),pageResult.getPageSize());
        //构建查询的条件构造器
        QueryWrapper<Patient> queryWrapper = new QueryWrapper<>();
        //根据判断条件决定是否拼接where条件
        queryWrapper.like(StringUtils.hasLength(pageResult.getQuery()),"username", pageResult.getQuery());
        //MP提供的分页查询方法,返回值是Page分页对象 其中包含分页的数据结果信息
        page = patientMapper.selectPage(page, queryWrapper);
        long total = page.getTotal();
        List<com.tian.pojo.Patient> records = page.getRecords();
        return pageResult.setRows(records).setTotal(total);

    }

    @Override
    @Transactional
    public void addPatient(Patient patient) {
        System.out.println(patient);
        patientMapper.insert(patient);
    }

    @Override
    public void updataPatient(Patient patient) {
        patientMapper.updateById(patient);
    }

    @Override
    public void deletePatient(Integer id) {
        System.out.println(id);
        patientMapper.deleteById(id);
    }


}
